﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Litografia.BusinessComponents;
using Litografia.BusinessEntities;

namespace Litografia.UI.UserControls
{
    public enum Criteria
	{
	    Id,
        Name
	}

    public partial class SearchableTextBox : UserControl
    {
        public SearchableTextBox()
        {
            InitializeComponent();
        }

        #region Declarations

        public event EventHandler SelectionChangedEvent;
        public event EventHandler ValidatedEvent;
        private bool loadingSearchResult;
        private const Keys searchKey = Keys.Enter;

        #endregion
        
        #region Properties: Public

        public string SelectedId { get; set; }
        public string SelectedCode { get; set; }
        public string SelectedName { get; set; }

        public override string Text
        {
            get { return textBox.Text; }
            set { textBox.Text = value; }
        }

        [Category("Data"), DisplayName("Entity")]
        public EntityType Entity { get; set; }
        
        [Category("Data"), DisplayName("SearchBy")]
        public Criteria SearchBy { get; set; }

        #endregion

        #region Event Handlers

        private void SearchableTextBox_Load(object sender, EventArgs e)
        {
            ConfigureView();
            LimpiaSeleccion();
        }

        private void SearchableTextBox_SizeChanged(object sender, EventArgs e)
        {
            const int buttonWidth = 20;
            const int labelWidth = 10;
            textBox.Size = new Size(Size.Width - buttonWidth - labelWidth - 2, Size.Height);
            button.Size = new Size(buttonWidth, Size.Height);
            invalidoLabel.Size = new Size(labelWidth, Size.Height);
            button.Location = new Point(Size.Width - buttonWidth - labelWidth, 0);
            invalidoLabel.Location = new Point(Size.Width - labelWidth, 0);
            Refresh();
        }

        private void button_MouseHover(object sender, EventArgs e)
        {
            button.BackgroundImage = Images.qfindhover;
        }

        private void button_MouseLeave(object sender, EventArgs e)
        {
            button.BackgroundImage = Images.qfind;
        }

        private void textBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == searchKey)
                if (!ValidateEntry())
                    button.PerformClick();
                else
                    SendKeys.Send("{TAB}");
        }

        private void button_Click(object sender, EventArgs e)
        {
            var form = new SearchForm(Entity);
            if (form.ShowDialog(ParentForm) == DialogResult.OK)
            {
                AsignaSeleccion(new BEEntity(form.SelectedId, form.SelectedName));
                loadingSearchResult = true;
                textBox.Text = form.SelectedId;
                loadingSearchResult = false;
            }
        }

        private void textBox_TextChanged(object sender, EventArgs e)
        {
            if (!loadingSearchResult)
                LimpiaSeleccion();
        }

        private void SearchableTextBox_Validated(object sender, EventArgs e)
        {
            ValidateEntry();
        }

        #endregion

        #region Methods: Non-Public

        private bool ValidateEntry()
        {
            bool result = false;
            string filtro = textBox.Text.Trim();
            if (!String.IsNullOrEmpty(filtro))
            {
                BEEntity entity = BCEntity.GetItemById(Entity, filtro);
                if (entity != null)
                {
                    AsignaSeleccion(entity);
                    textBox.Text = entity.Id;
                    invalidoLabel.Visible = false;
                    result = true;
                }
                else
                {
                    invalidoLabel.Visible = true;
                    LimpiaSeleccion();    
                }
            }
            if (ValidatedEvent != null)
                ValidatedEvent(this, new EventArgs());
            
            return result;
        }

        private void ConfigureView()
        {
            button.BackgroundImage = Images.qfind;
        }

        private void AsignaSeleccion(BEEntity item)
        {
            SelectedId = item.Id;
            SelectedName = item.Nombre;
            if (SelectionChangedEvent != null)
                SelectionChangedEvent(this, new EventArgs());
        }

        private void LimpiaSeleccion()
        {
            SelectedId = "";
            SelectedCode = "";
            SelectedName = "";
            if (SelectionChangedEvent != null)
                SelectionChangedEvent(this, new EventArgs());
        }

        #endregion

    }
}
